package com.soft.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.soft.entity.Goods;
import com.soft.entity.Goodsloc;
import com.soft.mapper.GoodsMapper;
import com.soft.mapper.GoodslocMapper;

	@Service
	public class GoodsService {
	@Autowired
	private GoodsMapper goodsMapper;
	public List<Goods> queryAll(){
	List<Goods> goodsList = goodsMapper.selectList(null);
	return goodsList;
	}
    public List<Goods> queryByname(String name){
    	QueryWrapper<Goods> qw =new QueryWrapper<>();
    	qw.eq("goods_name", name);
    	List<Goods> goodslist =goodsMapper.selectList(qw);
    	return goodslist;
    }
    public int insertOne(String name,String price) {
    	Goods goods=new Goods();
		goods.setGname(name);
		goods.setGprice(price);
		int num = goodsMapper.insert(goods);
		return num;
    }
    public int deleteById(Long id) {
    	Goods goods =new Goods();
    	goods.setGid(id);
    	int num =goodsMapper.deleteById(goods);
    	return num;
    }
    public int updateById(Long id,String name,String price) {
    	Goods goods=new Goods();
    	goods.setGid(id);
		goods.setGname(name);
		goods.setGprice(price);
		int num = goodsMapper.updateById(goods);
		return num;
    }
	
	@Autowired 
    private GoodslocMapper goodslocMapper;
    
    public List<Goods> queryByGoodslocName(String goodslocName){
    	QueryWrapper<Goodsloc> goodslocWrapper=new QueryWrapper<>();
    	goodslocWrapper.eq("goodsloc_name",goodslocName);
    	Goodsloc goodsloc =goodslocMapper.selectOne(goodslocWrapper);
    	QueryWrapper<Goods> goodsWrapper =new QueryWrapper<>();
    	goodsWrapper.eq("goodsloc_id",goodsloc.getId());
    	List<Goods> goodslist =goodsMapper.selectList(goodsWrapper);
    	return goodslist;
    }
    public int  updateGoods(String name,String price,String goodslocname) {
    	QueryWrapper<Goodsloc> goodslocWrapper=new QueryWrapper<>();
    	goodslocWrapper.eq("goodsloc_name",goodslocname);
    	Goodsloc goodsloc=goodslocMapper.selectOne(goodslocWrapper);
    	Goods goods=new Goods();
    	QueryWrapper<Goods> goodsWrapper = new QueryWrapper<>();
    	goodsWrapper.eq("goods_name",name);
    	goodsWrapper.eq("goodsloc_id",goodsloc.getId());
    	goods.setGprice(price);
    	int num=goodsMapper.update(goods,goodsWrapper);
    	return num;
    	
    }
    
    
    
    
    
    
    
    
    
    

}
